package texts;

import moment.Utiles;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class ListPage extends Utiles {
    public static String url = "http://110.41.84.205:9091/index.html";
    public ListPage() {
        super(url);
    }
    public void listPageRight() {
        String title = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        assert "首页".equals(title);
    }
    public void listPageFail() throws InterruptedException {
        // 建立显示等待3秒
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        // 打开发布帖子
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"),"发新贴"));
        String text = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2")).getText();
        // 滚动页面
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //向下拉到底
        js.executeScript("window.scrollTo(0,100000)");

        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#article_post_submit")));
        Thread.sleep(300);
        driver.findElement(By.cssSelector("#article_post_submit")).click();
        String text1 = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert "提示".equals(text1);
    }

    /**
     * 发布成功
     * @throws InterruptedException
     */
    public void listPageSec() throws InterruptedException {

        driver.navigate().refresh(); // 刷新页面
        // 重新进入发布页面
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        // 输入内容
        WebElement element = driver.findElement(By.cssSelector("#article_post_title"));
        element.sendKeys("自动化测试");

        // 将WebDriver转换为JavaScriptExecutor
        JavascriptExecutor js = (JavascriptExecutor) driver;

        // 定位输入框
        WebElement inputElement = driver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div"));
        // 模拟键盘点击和输入
        Actions action = new Actions(driver);
        action.sendKeys(inputElement, "发布测试").build().perform();
        // 下滑页面
        js.executeScript("window.scrollTo(0,100000)");
        Thread.sleep(300);
        driver.findElement(By.cssSelector("#article_post_submit")).click();
    }
}
